package com.chj.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.chj.constants.Constants;
import com.chj.dao.NetWorkDAO;
import com.chj.entity.NetWork;
import com.chj.mapper.NetWorkMapper;
import com.chj.util.GenerateSequenceUtil;
import com.etc.jdbc.util.JDBCUtils;

@Repository
public class NetWorkDAOImpl implements NetWorkDAO {

    @Override
    public int Save(NetWork netWork) {

        String sql = "INSERT INTO `t_network` " +
        		" (`network_id`" +
        		", `network_name`" +
        		", `network_park_size`" +
        		", `network_province`" +
        		", `network_city`" +
        		", `network_area`" +
        		", `network_address`" +
        		", `network_price`" +
        		", `network_time_begin`" +
        		", `network_time_end`" +
        		", `network_type`" +
        		", `create_date`" +
        		", `modify_date`" +
        		", `delete_date`" +
        		", `status`)" +
        		" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        return JDBCUtils.update(sql
                               ,GenerateSequenceUtil.getUUID()//network_id
                               ,netWork.getNetworkName()//network_name
                               ,netWork.getNetworkParkSize()//network_park_size
                               ,netWork.getNetworkProvince()//network_province
                               ,netWork.getNetworkCity()//network_city
                               ,netWork.getNetworkArea()//network_area
                               ,netWork.getNetworkAddress()//network_address
                               ,netWork.getNetworkPrice()//network_price
                               ,netWork.getNetworkTimeBegin()//network_time_begin
                               ,netWork.getNetworkTimeEnd()//network_time_end
                               ,netWork.getNetworkType()//network_type
                               ,new Date()//create_date
                               ,Constants.NULL_DATE//modify_date
                               ,Constants.NULL_DATE//delete_date
                               ,Constants.ROW_STATUS_EFFECTIVE//status
                               );
    }

    @Override
    public boolean isNetWorkIsExist(String netWorkName) {

        String sql = "SELECT COUNT(1) FROM t_network T  WHERE T.network_name = ?";

        int queryForInt = JDBCUtils.queryForInt(sql, netWorkName);
        
        if (queryForInt == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<NetWork> getNetWorkList(String networkName
                                      , Integer networkParkSize
                                      , String networkProvince
                                      , String networkCity
                                      , String networkArea
                                      , String networkPrice
                                      , String networkTimeBegin
                                      , String networkTimeEnd
                                      , Integer networkType
                                      , int page
                                      , int rows) {
        
        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select *                           ";
        sql += "   from t_network t                 ";
        sql += "  where t.network_type = ?          ";

        paramsList.add(networkType);

        if (StringUtils.isNotBlank(networkName)) {
            sql += " and t.network_name like ? ";
            paramsList.add(networkName+Constants.SQL_LIKE);
        }

        if (networkParkSize != 0) {
            sql += " and t.network_park_size = ? ";
            paramsList.add(networkParkSize);
        }

        if (StringUtils.isNotBlank(networkProvince)) {
            sql += " and t.network_province like ? ";
            paramsList.add(networkProvince + Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(networkCity)) {
            sql += " and t.network_city like ? ";
            paramsList.add(networkCity+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(networkArea)) {
            sql += " and t.network_area like ? ";
            paramsList.add(networkArea+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(networkPrice)) {
            sql += " and t.network_price  =  ? ";
            paramsList.add(networkPrice);
        }

        if (StringUtils.isNotBlank(networkTimeBegin)) {
            sql += " and t.network_time_begin >= ? ";
            paramsList.add(networkTimeBegin);
        }

        if (StringUtils.isNotBlank(networkTimeEnd)) {
            sql += "    and t.network_time_end <= ? ";
            paramsList.add(networkTimeEnd);
        }
        if (page != 0 && rows != 0) {
            sql += " limit ?,?";
            paramsList.add((page-1)*rows);
            paramsList.add((page)*rows);
        }

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForList(sql, new NetWorkMapper(), params);
    }

    @Override
    public int getNetWorkListSize(String networkName
                                  , Integer networkParkSize
                                  , String networkProvince
                                  , String networkCity
                                  , String networkArea
                                  , String networkPrice
                                  , String networkTimeBegin
                                  , String networkTimeEnd
                                  , Integer networkType) {
        
        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select count(1)                    ";
        sql += "   from t_network t                 ";
        sql += "  where t.network_type = ?          ";

        paramsList.add(networkType);

        if (StringUtils.isNotBlank(networkName)) {
            sql += " and t.network_name like ? ";
            paramsList.add(networkName+Constants.SQL_LIKE);
        }

        if (networkParkSize != 0) {
            sql += " and t.network_park_size = ? ";
            paramsList.add(networkParkSize);
        }

        if (StringUtils.isNotBlank(networkProvince)) {
            sql += " and t.network_province like ? ";
            paramsList.add(networkProvince + Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(networkCity)) {
            sql += " and t.network_city like ? ";
            paramsList.add(networkCity+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(networkArea)) {
            sql += " and t.network_area like ? ";
            paramsList.add(networkArea+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(networkPrice)) {
            sql += " and t.network_price  =  ? ";
            paramsList.add(networkPrice);
        }

        if (StringUtils.isNotBlank(networkTimeBegin)) {
            sql += " and t.network_time_begin >= ? ";
            paramsList.add(networkTimeBegin);
        }

        if (StringUtils.isNotBlank(networkTimeEnd)) {
            sql += "    and t.network_time_end <= ? ";
            paramsList.add(networkTimeEnd);
        }

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForInt(sql, params);
    }

    @Override
    public List<NetWork> getNetworkAllList() {

        String sql = "";

        sql += " select *         ";
        sql += " from t_network t ";

        return JDBCUtils.queryForList(sql, new NetWorkMapper());
    }

    @Override
    public int deleteNetwork(String networkId) {

        String sql = " delete from t_network where network_id = ?";
        
        return JDBCUtils.update(sql, networkId);
    }

    @Override
    public NetWork get(String networkId) {

        String sql = "select * from t_network where network_id = ?";

        return JDBCUtils.queryForObject(sql, new NetWorkMapper(), networkId);
    }

    @Override
    public int modifyNetwork(NetWork dbNetwork) {

        String sql = "UPDATE `t_network` SET " +
        		"  `network_name`=?" +
        		", `network_park_size`=?" +
        		", `network_province`=?" +
        		", `network_city`=?" +
        		", `network_area`=?" +
        		", `network_address`=?" +
        		", `network_price`=?" +
        		", `network_time_begin`=?" +
        		", `network_time_end`=?" +
        		", `modify_date`=?" +
        		" WHERE (`network_id`=?)";
        
        return JDBCUtils.update(sql, dbNetwork.getNetworkName()
                                   , dbNetwork.getNetworkParkSize()
                                   , dbNetwork.getNetworkProvince()
                                   , dbNetwork.getNetworkCity()
                                   , dbNetwork.getNetworkArea()
                                   , dbNetwork.getNetworkAddress()
                                   , dbNetwork.getNetworkPrice()
                                   , dbNetwork.getNetworkTimeBegin()
                                   , dbNetwork.getNetworkTimeEnd()
                                   , new Date()
                                   , dbNetwork.getNetworkId());
    }

}
